package com.gf.research.cockpit.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.type.TypeHandlerRegistry;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;
import java.util.Properties;

/**
 * 投研平台的数据库配置
 *
 * @Author: wangjunchao(王俊超)
 * @Date: 2018-07-13 10:23
 **/
@Configuration
@MapperScan(
        basePackages = "com.gf.research.cockpit.sirm.mapper",
        sqlSessionTemplateRef = "cockpitSirmSqlSessionTemplate")
public class CockpitSirmDataSourceConfig {

    @Bean(name = "cockpitSirmDataSource")
    @Qualifier("cockpitSirmDataSource")
    @ConfigurationProperties("datasource.druid.research-cockpit.sirm")
    public DruidDataSource cockpitSirmDataSource() {
        return DruidDataSourceBuilder.create().build();
    }

    @Bean(name = "cockpitSirmGlobalProperties")
    @ConfigurationProperties("datasource.research-cockpit.sirm.mybatis.global")
    public Properties globalProperties() {
        return new Properties();
    }

    @Bean(name = "cockpitSirmSqlSessionFactory")
    public SqlSessionFactory sirmSqlSessionFactory(
            @Qualifier("cockpitSirmDataSource") DataSource dataSource,
            @Value("${sirm.cockpit.mybatis.mapper-locations}") String mapperLocation,
            @Value("${mybatis.type-aliases-package}") String typeAliasesPackage,
            @Qualifier("cockpitSirmGlobalProperties") Properties properties)
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources(mapperLocation));
        bean.setTypeAliasesPackage(typeAliasesPackage);
        bean.setConfigurationProperties(properties);

        // 开启mybatis的下划线转驼峰
        bean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);

        registryEnumHandler(bean.getObject().getConfiguration().getTypeHandlerRegistry());

        return bean.getObject();
    }

    /**
     * 注册mybatis枚举转换类型
     *
     * @param typeHandlerRegistry
     * @exception Exception
     */
    private void registryEnumHandler(TypeHandlerRegistry typeHandlerRegistry) {

    }

    @Bean(name = "cockpitSirmDataSourceTransactionManager")
    public DataSourceTransactionManager sirmDataSourceTransactionManager(
            @Qualifier("cockpitSirmDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "cockpitSirmSqlSessionTemplate")
    public SqlSessionTemplate sirmSqlSessionTemplate(
            @Qualifier("cockpitSirmSqlSessionFactory") SqlSessionFactory sqlSessionFactory)
            throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
